<?php

namespace app\api\model;

use think\Model;

class UserBankModel extends Model{
	protected $table = 'ly_user_bank';

	/** 添加银行卡 **/
	public function addBankCard(){
		//获取参数	token、name、card_no、bank_name、bank_branch_name
		$post 		= input('post.');

		$token		= input('post.token/s');
		$userArr	= explode(',',auth_code($token,'DECODE'));
		$uid		= $userArr[0];
        $username		= $userArr[1];
		$lang		= (input('post.lang')) ? input('post.lang') : 'kr';	// 语言类型

        $bank_name			= input('post.category/s');	// 银行名称
        $card_no			= input('post.address/s');		// 银行卡账号
        $bank_id			= input('post.bank_id/s');	// 银行名称
		$name				= input('post.name/s');			// 持卡人姓名
		$bank_branch_name	= input('post.bank_branch_name/s');	// 支行名称
		$remark				= input('post.remark/s');			// 备注


		// 绑定新增银行卡
		$bankCard_arr = array(
			'uid'				=> $uid,
			'name'				=> $username,		// 持卡人姓名
			'bid'				=> 0,
			'card_no'			=> $card_no,	// 银行卡卡号
			'bank_name'			=> $bank_name,			// 银行名称
			'bank_branch_name'	=> $bank_name,	// 支行名称
			'remark'			=> '',	// 备注
            'status'			=> 0,	// 待审核
			'add_time'			=> time(),
			'lang'				=> $lang,
		);
        $bank = $this->where(['uid'=>$uid,'bank_name'=>$bank_name])->find();
        if ($bank){
            $ok = $this->where(['uid'=>$uid,'bank_name'=>$bank_name])->update(["card_no"=>$card_no]);
        }else {
            $ok = $this->insert($bankCard_arr);
        }
		if(!$ok)	return	['code'	=> 0, 'code_dec' => config($lang.'.lang')[41]];
		return ['code'	=> 1, 'code_dec' => config($lang.'.lang')[42]];
	}


	/** 获取银行卡列表 **/
	public function getBankCardList(){
		// 获取参数
		$token 		= input('post.token/s');
		$userArr	= explode(',',auth_code($token,'DECODE'));
		$uid		= $userArr[0];
		$lang		= (input('post.lang')) ? input('post.lang') : 'cn';	// 语言类型
		// 获取数据
		$bankCard_arr = $this->where('uid',$uid)->where('status',1)->order('id desc')->select()->toArray();

		if(!count($bankCard_arr))	return	['code'	=> 0, 'code_dec' => config($lang.'.lang')[45]];

		// 返回数组
		$data['code'] = 1;
		foreach($bankCard_arr as $key =>$value){
			$data['data'][$key]['id']			= $value['id'];
			$data['data'][$key]['card_no']		= $value['card_no'];		// 银行卡账号
			$data['data'][$key]['bank_name']	= $value['bank_name'];		// 银行名称
			$data['data'][$key]['bank_branch_name']	= $value['bank_branch_name'];		// 支行名称
			$data['data'][$key]['name']			= $value['name'];			// 持卡人姓名
			$data['data'][$key]['remark']		= $value['remark'];			// 备注
		}

		return $data;
	}


	/** 获取银行卡信息 **/
	public function getBankCardInfo(){
		// 获取参数
		$token 		= input('post.token/s');
		$userArr	= explode(',',auth_code($token,'DECODE'));
		$uid		= $userArr[0];
		$card_no	= input('post.card_no/d','','strip_tags,trim');	// 银行卡号
		$lang		= (input('post.lang')) ? input('post.lang') : 'cn';	// 语言类型
		// 获取数据
		$bankCardInfo = $this->where(['uid'=>$uid,'card_no'=>$card_no])->find();
		if(!$bankCardInfo)	return	['code'	=> 0, 'code_dec' => config($lang.'.lang')[45]];

		// 返回数组
		$data['data']['id']			= $bankCardInfo['id'];
		$data['data']['card_no']	= $bankCardInfo['card_no'];	// 银行卡账号
		$data['data']['bank_name']	= model('Bank')->where('id',$bankCardInfo['bid'])->value('bank_name');	// 银行名称
		$data['data']['bank_branch_name']	= $bankCardInfo['bank_branch_name'];	// 支行名称
		$data['data']['name']		= $bankCardInfo['name'];		// 持卡人姓名
		$data['data']['remark']		= $bankCardInfo['remark'];		// 备注
		$data['data']['status']		= $bankCardInfo['status'];		// 银行卡状态：1=正常；2=锁定；3=删除

		return $data;
	}


	/**  修改银行卡信息	 **/
	public function changeBankCardInfo(){
		///获取参数
		$token = input('post.token/s');
		$userArr	= explode(',',auth_code($token,'DECODE'));
		$uid		= $userArr[0];
		$card_no	= input('post.card_no/d','','strip_tags,trim');	// 银行卡号
		$act		= input('post.action/d');	// 银行卡修改操作
		$lang		= (input('post.lang')) ? input('post.lang') : 'cn';	// 语言类型
		//获取数据
		$bankCardId = $this->where(['uid'=>$uid,'card_no'=>$card_no])->value('id');
		if(!$bankCardId)	return	['code'	=> 0, 'code_dec' => config($lang.'.lang')[41]];

		switch($act){
			case '1':	// 银行卡删除
				$isDel_bankInfo	= $this->where('id',$bankCardId)->update(['status'=>3]);

				if(!$isDel_bankInfo)	return	['code'	=> 0, 'code_dec' => config($lang.'.lang')[41]];

				return ['code' => 1, 'code_dec'	=> config($lang.'.lang')[42]];
			break;
			case '2':	// 银行卡锁定
				$isLocking		= $this->where('id',$bankCardId)->update(['status'=>2]);

				if(!$isLocking)		return	['code'	=> 0, 'code_dec' => config($lang.'.lang')[41]];

				return ['code' => 1, 'code_dec'	=> config($lang.'.lang')[42]];
			break;
			case '3':	// 银行卡解锁
				$isUnlocking		= $this->where('id',$bankCardId)->update(['status'=>1]);

				if(!$isUnlocking)	return	['code'	=> 0, 'code_dec' => config($lang.'.lang')[41]];

				return ['code' => 1, 'code_dec'	=> config($lang.'.lang')[42]];
			break;
		}
	}
}
